package edu.example.student.servlet;

import edu.example.common.PowerCheck;
import edu.example.student.beans.Building;
import edu.example.student.beans.Student;
import edu.example.student.dao.BuildingDao;
import edu.example.student.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentupdate")
public class StudentUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*权限*/
        if (!PowerCheck.isAdmin(req)) {
            req.setAttribute("reMessage", "无权访问");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
            return;
        }
        BuildingDao buildingDao = new BuildingDao();
        List<Building> all = buildingDao.findAll();
        StudentDao studentDao=new StudentDao();
        Integer stuID= Integer.parseInt(req.getParameter("id"));
        System.out.println(stuID);
        List<Student> selects=studentDao.selects(stuID);
//        Student student=new Student();
        Student student = selects.get(0);
        req.setAttribute("stuID", stuID);
        req.setAttribute("stucode",student.getStucode());
        req.setAttribute("name", student.getName());
        req.setAttribute("gender", student.getGender());
        req.setAttribute("password", student.getPassword());
        req.setAttribute("phone", student.getPhone());
        req.setAttribute("rooms_card", student.getRooms_card());
        req.setAttribute("buildings_card", student.getBuildings_card());

        String build = student.getBuildings_card();
        Building currentBuilding = null;
        for (Building building : all) {
            if (building.getBuildingCard().equals(build)) {
                currentBuilding = building;
                break;
            }
        }
        all.remove(currentBuilding);
        all.add(0, currentBuilding);

        req.setAttribute("builds", all);
        req.getRequestDispatcher("/student/update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*权限*/
        if (!PowerCheck.isAdmin(req)) {
            req.setAttribute("reMessage", "无权访问");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
            return;
        }
        String name=req.getParameter("stuname");
        String password=req.getParameter("stupwd");
        String phone=req.getParameter("stutel");
        Integer stuID= Integer.parseInt(req.getParameter("stuID"));
        StudentDao studentDao=new StudentDao();
        Integer code=studentDao.updateList(stuID,password,name,phone);
        req.setAttribute("update",code);
        resp.sendRedirect("studentlist");
    }
}
